package interaction.event;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class EventDispatcher {
	private Map<String, List<EventListener<?>>> listeners = new HashMap<String, List<EventListener<?>>>();
	
	public <T> EventDispatcher attach(String type, EventListener<T> listener)
	{
		if (!listeners.containsKey(type)) {
			listeners.put(type, new LinkedList<EventListener<?>>());
		}
		listeners.get(type).add(listener);
		return this;
	}
	
	public <T> void notify(Event<T> e)
	{
		if (listeners.containsKey(e.getType())) {
			for (EventListener<?> l : listeners.get(e.getType())) {
				((EventListener<T>) l).notify(e);
			}
		}
	}
}
